<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：Credit.class.php
 * @类功能: 信用卡
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\api\controller;

use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\bank\IBankCache;
use app\common\cache\credit\ICreditCache;
use app\common\cache\credit\ICreditFavoriteCache;
use app\common\cache\credit\ICreditRecommendCache;
use app\common\cache\credit\ICreditTagCache;
use app\common\cache\user\IUserCache;
use app\common\controller\ApiBaseController;
use app\common\enum\CreditLevel;
use app\common\enum\CreditPurpose;
use app\common\enum\CreditStatus;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsCredit;
use app\common\enum\IsDelete;
use app\common\enum\IsTrack;
use app\common\ext\IDb;
use app\common\ext\ILog;
use app\common\ext\IUrl;

class Credit extends ApiBaseController{
    /**
     * @功能：取得推荐信用卡
     * @开发者：cxl
     */
    public function recommend(){
        //接收参数
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编号不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");
        $recommend_location = self::get_data('recommend_location',self::NOT_EMPTY,"推荐位置不能为空！");

        //判断列表是否存在
        $credit_recommend_list = ICreditRecommendCache::getList($recommend_location,$province_id,$city_id);
        if($credit_recommend_list === false){
            self::set_code(self::ERR_0001);
            self::set_msg("取得推荐信用卡列表失败！");
            self::send();
        }

        //判断列表是否存在
        if(empty($credit_recommend_list) || !is_array($credit_recommend_list)) {
            $credit_recommend_list = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_recommend_list",$credit_recommend_list);
        self::send();
    }

    /**
     * @功能：信用卡搜索选项
     * @开发者：cxl
     */
    public function credit_search(){
        //设置用途说明数据
        $credit_purpose_note = array(
            CreditPurpose::TX=>"超高提现比例",
            CreditPurpose::CS=>"超市商场购物优惠",
            CreditPurpose::JD=>"酒店出行优惠",
            CreditPurpose::HK=>"里程兑换、免费延迟险",
            CreditPurpose::CZ=>"加油优惠、办ETC免费等",
            CreditPurpose::JW=>"境外消费享受0货币兑换手续费",
            CreditPurpose::WL=>"QQ、淘宝购物积分网购优惠",
            CreditPurpose::KT=>"卡面是卡通形象等",
            CreditPurpose::TE=>"首笔取现免费等功能",
            CreditPurpose::NR=>"消费多倍积分、美容等服务优惠",
            CreditPurpose::BZ=>"银行标准服务"
        );

        //设置等级说明数据
        $credit_level_note = array(
            CreditLevel::PK=>"71.9%的人额度在1000-30000之间",
            CreditLevel::JK=>"80.1%的人额度在5000-50000之间",
            CreditLevel::BJK=>"年费必须交",
        );


        //判断类型数据
        $search_type = array(
            0 => array("search_type" => 1,"search_type_name"=>"全部用途","search_item"=>array()),
            1 => array("search_type" => 2,"search_type_name"=>"全部等级","search_item"=>array()),
            2 => array("search_type" => 3,"search_type_name"=>"更多","search_item"=>array()),
        );

        //取得名次
        $search_type_name = array(
            "credit_purpose"=>"用途",
            "credit_level"=>"等级",
            "credit_fee"=>"年费",
            "credit_currency"=>"币种",
            "credit_agency"=>"组织",
            "credit_privilege"=>"特权",
            "credit_color"=>"卡面颜色",
            "credit_type"=>"卡面类型",
//            "credit_tag"=>"标签",
        );

        //设置用途
        $search = array(
            array("search_type"=>0,"name"=>"credit_purpose","note"=>$credit_purpose_note),
            array("search_type"=>1,"name"=>"credit_level","note"=>$credit_level_note),
            array("search_type"=>2,"name"=>"credit_fee",),
            array("search_type"=>2,"name"=>"credit_currency",),
            array("search_type"=>2,"name"=>"credit_agency",),
            array("search_type"=>2,"name"=>"credit_privilege",),
            array("search_type"=>2,"name"=>"credit_color",),
            array("search_type"=>2,"name"=>"credit_type",),
//            array("search_type"=>2,"name"=>"credit_tag",),
        );

        //循环添加数据
        foreach($search as $item){
            //初期化变量
            $data = array();
            $data['filed'] = $item['name'];
            $data['name'] = $search_type_name[$item['name']];

            //判断是否为标签
            if(trim($item['name']) == "credit_tag") {
                //取得信用卡标签列表
//                $credit_tag_list = ICreditTagCache::getList();
//                $credit_tag_list = ICreditTagCache::getList();

//                //判断数据是否正确
//                if(!empty($credit_tag_list) && is_array($credit_tag_list)){
//                    foreach($credit_tag_list as $tag) {
//                        $data['value'][] = array("item" => $tag['credit_tag_id'], "title" => $tag['credit_tag_name'], "note" => "");
//                    }
//                }
            }else{
                //判断数据是否存在
                if (!empty(HtmlEnumValue::$enum_value[$item['name']]) && is_array(HtmlEnumValue::$enum_value[$item['name']])) {
                    //设置初期値
                    $credit_item_data = HtmlEnumValue::$enum_value[$item['name']];

                    //设置值
                    foreach ($credit_item_data as $key => $val) {
                        //判断说明是否存在
                        if (empty($item['note']) || empty($item['note'][$key])) {
                            $data['value'][] = array("item" => $key, "title" => $val, "note" => "");
                        } else {
                            $data['value'][] = array("item" => $key, "title" => $val, "note" => $item['note'][$key]);
                        }
                    }
                }
            }

            //设置用途
            $search_type[$item['search_type']]['search_item'][] = $data;
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_credit_item_list",$search_type);
        self::send();
    }

    /**
     * @功能：信用卡列表
     * @开发者：cxl
     */
    public function credit_search_list(){
        //接收参数
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编号不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");
        $bank_id  = self::get_data('bank_id');
        $credit_purpose  = self::get_data('credit_purpose');
        $credit_level  = self::get_data('credit_level');
        $credit_fee  = self::get_data('credit_fee');
        $credit_currency  = self::get_data('credit_currency');
        $credit_agency  = self::get_data('credit_agency');
        $credit_privilege  = self::get_data('credit_privilege');
        $credit_color  = self::get_data('credit_color');
        $credit_type  = self::get_data('credit_type');
        $credit_tag  = self::get_data('credit_tag');
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $credit_product_where['is_delete'] = IsDelete::No;
        $credit_product_where['credit_status'] = CreditStatus::release;
        if(!empty($bank_id)) $credit_product_where['bank_id'] = $bank_id;
        if(!empty($credit_purpose)) $credit_product_where['credit_purpose'] = $credit_purpose;
        if(!empty($credit_level)) $credit_product_where['credit_level'] = $credit_level;
        if(!empty($credit_fee)) $credit_product_where['credit_fee'] = $credit_fee;
        if(!empty($credit_currency)) $credit_product_where[''] = array("exp","find_in_set({$credit_currency},credit_currency)");
        if(!empty($credit_agency)) $credit_product_where['credit_agency'] = $credit_agency;
        if(!empty($credit_privilege)) $credit_product_where['credit_privilege'] = $credit_privilege;
        if(!empty($credit_color)) $credit_product_where['credit_color'] = $credit_color;
        if(!empty($credit_type)) $credit_product_where['credit_type'] = $credit_type;
        if(!empty($credit_tag)) $credit_product_where[''] = array("exp","find_in_set({$credit_tag},credit_tag_str)");

        //判断列表是否存在
        $credit_search_list = ICreditCache::getList($credit_product_where,$province_id,$city_id,$bottom_id);
        if($credit_search_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("搜索信用卡失败！");
            self::send();
        }

        //整理银行图片信息
        if(!is_array($credit_search_list)) {
            $credit_search_list = array();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_search_list",$credit_search_list);
        self::send();
    }

    /**
     * @功能：当前城市信用卡银行列表(信用卡全部搜索银行)
     * @开发者：cxl
     */
    public function bank(){
        //接收参数
        $city_id = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");

        //取得银行编号
        $bank_all_where['bank_type_id'] = array("eq",1);
        $bank_all_where['is_credit'] = array("eq",IsCredit::yes);
        $bank_all_list = IDb::getInstance('bank')
            ->setDbFiled("bank_id")
            ->setDbWhere($bank_all_where)
            ->setDbOrder("order_by desc,bank_id desc")
            ->sel();

        //判断数据是否存在
        if(empty($bank_all_list)){
            $bank_all_list = array();
        }else{
            $bank_all_list = array_column($bank_all_list,"bank_id");
        }

        //取得当前城市存在信用卡的银行
        $credit_product_area_where = "(cpa.city = '{$city_id}' or (cpa.province is null and cpa.city is null))";
        $bank_city_list = IDb::getInstance('credit_product as cp')
            ->setDbFiled("distinct cp.bank_id")
            ->setDbJoin("credit_product_area as cpa","cpa.credit_product_id=cp.credit_product_id")
            ->setDbWhere($credit_product_area_where)
            ->setDbOrder("order_by desc,bank_id desc")
            ->sel();

        //整理银行编号
        if(empty($bank_city_list)){
            $bank_city_list = array();
        }else{
            $bank_city_list = array_column($bank_city_list,"bank_id");
        }

        //整理银行列表
        $bank_list = array_merge($bank_city_list,$bank_all_list);
        $bank_list = array_unique($bank_list);
        $bank_where['is_delete'] = IsDelete::No;
        $bank_where['bank_id'] = array("in",$bank_list);
        $bank_list = IDb::getInstance('bank')
            ->setDbFiled("bank_id,bank_name,bank_url,bank_outline,pass_rate,ratify_velocity,general,gold,platinum,heat,specially,credit_schedule_url,is_hot")
            ->setDbWhere($bank_where)
            ->setDbOrder("order_by desc,bank_id")
            ->sel();

        //判断列表是否存在
        if($bank_list === false){
            self::set_code(self::ERR_0001);
            self::set_msg("取得当前城市信用卡银行列表！");
            self::send();
        }

        //整理银行图片信息
        foreach($bank_list as &$data){
            if($data['pass_rate'] < 2){
                $data['through_rate'] = '低';
            }elseif($data['pass_rate'] < 4){
                $data['through_rate'] = '中';
            }else{
                $data['through_rate'] = '高';
            }
            //判断是否存在信用卡
            if(in_array($data['bank_id'],$bank_city_list)){
                //整理数据
                $data['is_exists'] = "1";
            }else{
                //整理数据
                $data['is_exists'] = "0";
            }
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("bank_list",$bank_list);
        self::send();
    }

    /**
     * @功能：信用卡明细信息
     * @开发者：cxl
     */
    public function credit_details(){
        //接收参数
        $user_id = self::$uid;
        $credit_id  = self::get_data('credit_id',self::NOT_EMPTY,"信用卡编码不能为空！");

        //取得信用卡详情信息
        $credit_product_info = ICreditCache::getInfo($credit_id);

        //判断信用卡信息是否已经删除
        if($credit_product_info['is_delete'] == IsDelete::Yes){
            self::set_code(self::WARNING_0002);
            self::set_msg("信用卡尚未发布或者已经下架！");
            self::send();
        }

        //判断信用卡信息是否已经下降
        if($credit_product_info['credit_status'] != CreditStatus::release){
            self::set_code(self::WARNING_0002);
            self::set_msg("信用卡尚未发布或者已经下架！");
            self::send();
        }

        //整理币种信息
        if(!empty($credit_product_info['credit_currency'])) {
            //取得币种编号
            $credit_currency = explode(",",$credit_product_info['credit_currency']);

            //判断是否为数组
            if(!empty($credit_currency) && is_array($credit_currency)) {
                //整理信用卡币种数据
                foreach ($credit_currency as &$data) {
                    //判断数据是否为空
                    if (!empty(HtmlEnumValue::$enum_value['credit_currency'][$data])) {
                        $data = HtmlEnumValue::$enum_value['credit_currency'][$data];
                    }
                }

                //判断数据是否存在
                if(!empty($credit_currency) && is_array($credit_currency)) {
                    $credit_product_info['credit_currency'] = $credit_currency;
                }else{
                    $credit_product_info['credit_currency'] = array();
                }
            }else{
                $credit_product_info['credit_currency'] = array();
            }
        }else{
            $credit_product_info['credit_currency'] = array();
        }

        //整理状态数据
        if(!empty($credit_product_info['credit_fee'])) {$credit_product_info['credit_fee'] = HtmlEnumValue::$enum_value['credit_fee'][$credit_product_info['credit_fee']];}else{$credit_product_info['credit_fee'] = '';} //信用卡年费
        if(!empty($credit_product_info['credit_level'])) {$credit_product_info['credit_level'] = HtmlEnumValue::$enum_value['credit_level'][$credit_product_info['credit_level']];}else{$credit_product_info['credit_level'] = '';} //信用卡等级
        if(!empty($credit_product_info['credit_agency'])) {$credit_product_info['credit_agency'] = HtmlEnumValue::$enum_value['credit_agency'][$credit_product_info['credit_agency']];}else{$credit_product_info['credit_agency'] = '';} //信用卡发卡机构
        if(!empty($credit_product_info['credit_privilege'])) {$credit_product_info['credit_privilege'] = HtmlEnumValue::$enum_value['credit_privilege'][$credit_product_info['credit_privilege']];}else{$credit_product_info['credit_privilege'] = '';} //信用卡特权
        if(!empty($credit_product_info['credit_purpose'])) {$credit_product_info['credit_purpose'] = HtmlEnumValue::$enum_value['credit_purpose'][$credit_product_info['credit_purpose']];}else{$credit_product_info['credit_purpose'] = '';} //信用卡用途
        if(!empty($credit_product_info['credit_type'])) {$credit_product_info['credit_type'] = HtmlEnumValue::$enum_value['credit_type'][$credit_product_info['credit_type']];}else{$credit_product_info['credit_type'] = '';} //信用卡用途

        //整理json数据
//        if(!empty($credit_product_info['credit_condition_str'])) {$credit_product_info['credit_condition_str'] = json_decode($credit_product_info['credit_condition_str'],true);}else{$credit_product_info['credit_condition_str'] = array();} //申请条件
//        if(!empty($credit_product_info['credit_privilege_str'])) {$credit_product_info['credit_privilege_str'] = json_decode($credit_product_info['credit_privilege_str'],true);}else{$credit_product_info['credit_privilege_str'] = array();} //信用卡专属特权
//        if(!empty($credit_product_info['credit_base_str'])) {$credit_product_info['credit_base_str'] = json_decode($credit_product_info['credit_base_str'],true);}else{$credit_product_info['credit_base_str'] = array();} //基本信息(JSON)
//        if(!empty($credit_product_info['credit_fee_str'])) {$credit_product_info['credit_fee_str'] = json_decode($credit_product_info['credit_fee_str'],true);}else{$credit_product_info['credit_fee_str'] = array();} //费用信息(JSON)
//        if(!empty($credit_product_info['credit_discount_str'])) {$credit_product_info['credit_discount_str'] = json_decode($credit_product_info['credit_discount_str'],true);}else{$credit_product_info['credit_discount_str'] = array();} //优惠活动(JSON)
//        if(!empty($credit_product_info['credit_cheat_str'])) {$credit_product_info['credit_cheat_str'] = json_decode($credit_product_info['credit_cheat_str'],true);}else{$credit_product_info['credit_cheat_str'] = array();} //申卡秘籍(JSON)
//        if(!empty($credit_product_info['credit_interest_str'])) {$credit_product_info['credit_interest_str'] = json_decode($credit_product_info['credit_interest_str'],true);}else{$credit_product_info['credit_interest_str'] = array();} //分期利息(JSON)

        //处理标签信息
        if(!empty($credit_product_info['credit_tag_str'])) {
            //取得标签字符串
            $credit_product_info['credit_tag_str'] = explode(",",$credit_product_info['credit_tag_str']);

        }else{
            $credit_product_info['credit_tag_str'] = array();
        }

        //判断是否收藏
        if(empty($user_id)){
            $credit_product_info['is_favorite'] = 1;
        }else{
            //查询是否收藏
            $credit_favorite_where['is_delete'] = IsDelete::No;
            $credit_favorite_where['user_id'] = $user_id;
            $credit_favorite_where['credit_product_id'] = $credit_id;
            $credit_favorite_info = IDb::getInstance('credit_favorite')->setDbFiled("credit_favorite_id")->setDbWhere($credit_favorite_where)->row();
            if(!empty($credit_favorite_info['credit_favorite_id'])){
                $credit_product_info['is_favorite'] = 2;
            }else{
                $credit_product_info['is_favorite'] = 1;
            }
        }

        $credit_product_info['agent_admin_info'] = IAgentAdminCache::getInfoForDisplay($credit_product_info['agent_admin_id']);

        $credit_product_info['agent_admin_list'] = [$credit_product_info['agent_admin_info']];

        //取得信用卡分享地址
        $credit_product_info['share_url'] = IUrl::get("/wap/share/credit/credit_id/{$credit_id}.html");

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($credit_product_info);
        self::send();
    }

    /**
     * @功能：设置分享记录
     * @开发者：cxl
     */
    public function credit_share_log(){
        //接收参数
        $user_id = self::$uid;
        $share_type  = self::get_data('share_type',self::NOT_EMPTY,"分享类型不能为空！");
        $credit_product_id  = self::get_data('credit_product_id',self::NOT_EMPTY,"信用卡编号不能为空！");

        //设置分享记录
        $credit_share_data['share_type'] = $share_type;
        if(!empty($user_id))$credit_share_data['user_id'] = $user_id;
        $credit_share_data['credit_product_id'] = $credit_product_id;
        $credit_share_add = IDb::getInstance("credit_share")->setDbData($credit_share_data)->add();
        if($credit_share_add === false){
            self::set_code(self::ERR_0001);
            self::set_msg("设置分享日志失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置信用卡收藏
     * @开发者：cxl
     */
    public function credit_favorite(){
        //接收参数
        $user_id  = self::$uid;
        $credit_id  = self::get_data('credit_id',self::NOT_EMPTY,"信用卡编码不能为空！");

        //实例化对象
        $credit_product_info = ICreditCache::getInfo($credit_id);
        if(empty($credit_product_info['credit_product_id'])){
            self::set_code(self::WARNING_0001);
            self::set_msg("信用卡不存在！");
            self::send();
        }

        //判断是否已经收藏
        $credit_favorite_where['user_id'] = $user_id;
        $credit_favorite_where['credit_product_id'] = $credit_id;
        $credit_favorite_info = IDb::getInstance("credit_favorite")->setDbFiled("credit_favorite_id")->setDbWhere($credit_favorite_where)->row();
        if(empty($credit_favorite_info['credit_favorite_id'])) {
            //添加收藏信息
            $credit_favorite_data['user_id'] = $user_id;
            $credit_favorite_data['credit_product_id'] = $credit_id;
            $credit_favorite_add = IDb::getInstance("credit_favorite")->setDbData($credit_favorite_data)->add();
            if($credit_favorite_add === false) {
                self::set_code(self::WARNING_0002);
                self::set_msg("收藏失败！");
                self::send();
            }
        }else{
            //添加收藏信息
            $credit_favorite_data['is_delete'] = IsDelete::No;
            $credit_favorite_upd = IDb::getInstance("credit_favorite")->setDbData($credit_favorite_data)->setDbWhere($credit_favorite_where)->upd();
            if($credit_favorite_upd === false) {
                self::set_code(self::WARNING_0002);
                self::set_msg("收藏失败！");
                self::send();
            }

            //收藏编号
            $credit_favorite_add = $credit_favorite_info['credit_favorite_id'];
        }

        //清除缓存
        ICreditFavoriteCache::deleteList($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_favorite_id",intval($credit_favorite_add));
        self::send();
    }

    /**
     * @功能：取消信用卡收藏
     * @开发者：cxl
     */
    public function credit_favorite_cancel(){
        //接收参数
        $user_id  = self::$uid;
        $credit_id  = self::get_data('credit_id',self::NOT_EMPTY,"信用卡编码不能为空！");

        //实例化对象
        $credit_product_info = ICreditCache::getInfo($credit_id);
        if(empty($credit_product_info['credit_product_id'])){
            self::set_code(self::WARNING_0001);
            self::set_msg("信用卡不存在！");
            self::send();
        }

        //添加收藏信息
        $credit_favorite_where['user_id'] = $user_id;
        $credit_favorite_where['credit_product_id'] = $credit_id;
        $credit_favorite_data['is_delete'] = IsDelete::Yes;
        $credit_favorite_upd = IDb::getInstance("credit_favorite")->setDbData($credit_favorite_data)->setDbWhere($credit_favorite_where)->upd();
        if($credit_favorite_upd === false) {
            self::set_code(self::WARNING_0002);
            self::set_msg("收藏失败！");
            self::send();
        }

        //清除缓存
        ICreditFavoriteCache::deleteList($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：用户收藏信用卡列表
     * @开发者：cxl
     */
    public function user_credit_favorite(){
        //接收参数
        $user_id  = self::$uid;
        $bottom_id  = self::get_data('bottom_id');

        //判断列表是否存在
        $credit_list = ICreditFavoriteCache::getList($user_id,$bottom_id);
        if (empty($credit_list) || !is_array($credit_list)) {
            $credit_list = array();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_list",$credit_list);
        self::send();
    }

    /**
     * @功能：设置信用卡申请日志
     * @开发者：cxl
     */
    public function credit_apply(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $credit_id  = self::get_data('credit_id',self::NOT_EMPTY,"信用卡编码不能为空！");
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编码不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编码不能为空！");

        //判断产品是否存在
        $credit_product_info = ICreditCache::getInfo($credit_id);
        if(empty($credit_product_info) || empty($credit_product_info['credit_status']) || $credit_product_info['credit_status'] != CreditStatus::release){
            self::set_code(self::WARNING_0001);
            self::set_msg("信用卡申请失败！");
            self::send();
        }

        //开启事物出来
        IDb::dbStartTrans();

        //判断是否需要修改用户信息
        if(!empty($user_data)){
            $user_where['user_id'] = $user_id;
            $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
            if($user_upd === false){
                IDb::dbRollback();
                self::set_code(self::ERR_0001);
                self::set_msg("贷款产品申请失败！");
                self::send();
            }
        }

        //取得信用卡信息
        $credit_apply_where['user_id'] = $user_id;
        $credit_apply_where['credit_product_id'] = $credit_id;
        $credit_apply_info = IDb::getInstance("credit_apply")->setDbWhere($credit_apply_where)->row();
        if(empty($credit_apply_info)){
            //设置信用卡申请日志
            $credit_apply_data['agent_id'] = $credit_product_info['agent_id'];
            $credit_apply_data['agent_admin_id'] = $credit_product_info['agent_admin_id'];
            $credit_apply_data['user_id'] = $user_id;
            $credit_apply_data['credit_product_id'] = $credit_id;
            $credit_apply_data['province_id'] = $province_id;
            $credit_apply_data['city_id'] = $city_id;
            $credit_apply_data['credit_apply_time'] = get_date_time();
            $credit_apply_add = IDb::getInstance("credit_apply")->setDbData($credit_apply_data)->add();
            if($credit_apply_add === false){
                IDb::dbRollback();
                self::set_code(self::ERR_0003);
                self::set_msg("信用卡申请失败！");
                self::send();
            }

            $credit_apply_id = $credit_apply_add;

            //添加操作日志
            $credit_apply_log_add = ILog::CreditApplyLog($credit_apply_add,$user_id,null,null,"申请信用卡！");
            if ($credit_apply_log_add === false) {
                IDb::dbRollback();
                self::set_code(self::ERR_0004);
                self::set_msg("信用卡申请失败！");
                self::send();
            }

        }else{
            self::set_code(self::WARNING_0001);
            self::set_msg("信用卡已经申请过了！");
            self::send();
            //设置信用卡申请日志
//            $credit_apply_where['credit_apply_id'] = $credit_apply_info['credit_apply_id'];
//            $credit_apply_data['credit_apply_time'] = get_date_time();
//            $credit_apply_upd = IDb::getInstance("credit_apply")->setDbData($credit_apply_data)->setDbWhere($credit_apply_where)->upd();
//            if($credit_apply_upd === false){
//                IDb::dbRollback();
//                self::set_code(self::ERR_0005);
//                self::set_msg("信用卡申请失败！");
//                self::send();
//            }
//
//            $credit_apply_id = $credit_apply_info['credit_apply_id'];
//
//            //添加操作日志
//            $credit_apply_log_add = ILog::CreditApplyLog($credit_apply_info['credit_apply_id'],$user_id,null,null,"再次申请改信用卡！");
//            if ($credit_apply_log_add === false) {
//                IDb::dbRollback();
//                self::set_code(self::ERR_0006);
//                self::set_msg("信用卡申请失败！");
//                self::send();
//            }
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        ICreditCache::deleteInfo($credit_id);
        ICreditCache::deleteList();
        ICreditRecommendCache::deleteList();
        ICreditFavoriteCache::deleteListAll();
        IUserCache::deleteInfo($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_apply_id",$credit_apply_id);
        self::send();
    }

    /**
     * @功能：信用卡申请列表
     * @开发者：cxl
     */
    public function credit_apply_list(){
        //接收参数
        $user_id = self::$uid;
        $bottom_id  = self::get_data('bottom_id');

        //查询记录条数
        $credit_apply_where['ct.is_delete'] = IsDelete::No;
        $credit_apply_where['bk.is_delete'] = IsDelete::No;
        $credit_apply_where['cal.user_id'] = $user_id;
        if(!empty($bottom_id)) $credit_apply_where['cal.credit_apply_id'] = array("lt",$bottom_id);
        $credit_apply_list = IDb::getInstance("credit_apply as cal")
            ->setDbFiled("cal.credit_apply_id,bk.bank_id,bk.bank_name,ct.credit_product_id,ct.credit_product_name,ct.credit_product_url,cal.credit_apply_time")
            ->setDbJoin("credit_product as ct","cal.credit_product_id=ct.credit_product_id")
            ->setDbJoin("bank as bk","bk.bank_id=ct.bank_id")
            ->setDbWhere($credit_apply_where)
            ->setDbOrder("cal.credit_apply_id desc")
            ->setDbLimit(10)
            ->sel();

        //判断列表是否存在
        if($credit_apply_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得信用卡申请列表失败！");
            self::send();
        }

        //整理银行图片信息
        if(empty($credit_apply_list) || !is_array($credit_apply_list)) {
            $credit_apply_list = array();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_apply_log_list",$credit_apply_list);
        self::send();
    }

    /**
     * @功能：取得信用申请详情
     * @开发者：cxl
     */
    public function credit_apply_info(){
        //接收参数
        $user_id  = self::$uid;
        $credit_apply_id  = self::get_data('credit_apply_id');

        //设置查询条件
        $loan_apply_where['user_id'] = $user_id;
        $loan_apply_where['credit_apply_id'] = $credit_apply_id;
        $apply_info = IDb::getInstance("credit_apply")
            ->setDbWhere($loan_apply_where)
            ->row();

        //判断列表是否存在
        if(empty($apply_info) || !is_array($apply_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得申请记录失败！");
            self::send();
        }

        //设置查询条件
        $product_info = ICreditCache::getInfo($apply_info['credit_product_id']);
        if(empty($product_info['credit_product_id'])){
            self::set_code(self::WARNING_0002);
            self::set_msg("取得申请记录失败！");
            self::send();
        }

        //设置贷款机构名称
        $apply_info['credit_product_name'] = $product_info['credit_product_name'];
        $apply_info['credit_product_bank'] = IBankCache::getInfo($product_info['bank_id'],'bank_name') ;

        $apply_info['agent_admin_info'] = IAgentAdminCache::getInfoForDisplay($product_info['agent_admin_id']);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("apply_info",$apply_info);
        self::send();
    }

    /**
     * @功能：信用卡产品进度查询日志
     * @开发者：cxl
     */
    public function credit_apply_log(){
        //接收参数
        $apply_id  = self::get_data('credit_apply_id',self::NOT_EMPTY,"贷款产品申请编码不能为空！");
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $loan_apply_log_where['credit_apply_id'] = $apply_id;
        $loan_apply_log_where['is_track'] = IsTrack::Yes;
        if(!empty($bottom_id)) $loan_apply_log_where['credit_apply_log_id'] = array("lt",$bottom_id);

        $apply_log_list = IDb::getInstance("credit_apply_log")
            ->setDbFiled("credit_apply_log_id,operate_note,operate_time")
            ->setDbWhere($loan_apply_log_where)
            ->setDbOrder("credit_apply_log_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($apply_log_list as $key=>$value){
            $apply_log_list[$key]['video_list'] = array();
            $apply_log_list[$key]['audio_list'] = array();
            $apply_log_list[$key]['img_list'] = array();
        }

        //判断列表是否存在
        if($apply_log_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("信用卡申请进度查询失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_apply_log",$apply_log_list);
        self::send();
    }

    /**
     * @功能：申请进度查询银行列表
     * @开发者：cxl
     */
    public function bank_list(){
        //TODO 信用卡城市
        $city_id = null;

        //查询记录条数
        $bank_list = IBankCache::getCreditList($city_id);

        //整理银行图片信息
        if(empty($bank_list) || !is_array($bank_list)) {
            $bank_list = array();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("bank_list",$bank_list);
        self::send();
    }
    /**
     * @功能：银行网点接口
     * @开发者：cxl
     */
    public function bank_website(){
        //接收参数
        $bank_id  = self::get_data('bank_id',self::NOT_EMPTY,"银行编码不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编码不能为空！");
        $area_id  = self::get_data('area_id');
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $bank_website_where['bank_id'] = $bank_id;
        $bank_website_where['bank_city_id'] = $city_id;
        if(!empty($area_id)) $bank_website_where['bank_area_id'] = $area_id;
        if(!empty($bottom_id)) $bank_website_where['bank_website_id'] = array("lt",$bottom_id);
        $bank_website_list = IDb::getInstance("bank_website")
            ->setDbFiled("bank_website_id,bank_sub,concat(bank_area_name,bank_address) as bank_address")
            ->setDbWhere($bank_website_where)
            ->setDbOrder("bank_website_id desc")
            ->setDbLimit(10)
            ->sel();

        //判断列表是否存在
        if($bank_website_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得银行网点信息失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("bank_website_list",$bank_website_list);
        self::send();
    }
}
